﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BTL_SQL
{
    public partial class FormDiemHocSinh : Form
    {
        AccessData ac = new AccessData();
        public FormDiemHocSinh()
        {
            InitializeComponent();
        }

        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=trantai-pc;Initial Catalog=QUANLY_TRUONG_THPT;Integrated Security=True");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection con = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("Pro_Hienthi_DiemHs", con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dgDiem.DataSource = dt;
        }

        private void butThem_Click(object sender, EventArgs e)
        {
            SqlConnection con = getconnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("Pro_Them_DiemHS", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@mahs", SqlDbType.Int).Value = int.Parse(txtMahs.Text);
            cmd.Parameters.Add("@mamh", SqlDbType.NVarChar,10).Value = txtMamh.Text;
            cmd.Parameters.Add("@magv", SqlDbType.Int).Value = int.Parse(txtMagv.Text);
            cmd.Parameters.Add("@diemhs1", SqlDbType.Float).Value = float.Parse(txtDiemhs1.Text);
            cmd.Parameters.Add("@diemhs2", SqlDbType.Float).Value = float.Parse(txtDiemhs2.Text);
            cmd.Parameters.Add("@diemthihk", SqlDbType.Float).Value = float.Parse(txtDiemhk.Text);
            //cmd.Parameters.Add("@diemtk", SqlDbType.Float).Value = float.Parse(txtDiemtk.Text);
            cmd.ExecuteNonQuery();
            Form1_Load(sender, e);
        }

        private void butHien_Click(object sender, EventArgs e)
        {
            Form1_Load(sender, e);
            grThongTin1.Enabled = true;
            grThongTin2.Enabled = true;
            grTimKiem.Enabled = false;
        }

        private void butSua_Click(object sender, EventArgs e)
        {
            SqlConnection con = getconnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("Pro_Sua_DiemHS", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@mahs", SqlDbType.Int).Value = int.Parse(txtMahs.Text);
            cmd.Parameters.Add("@mamh", SqlDbType.NVarChar, 10).Value = txtMamh.Text;
            cmd.Parameters.Add("@magv", SqlDbType.Int).Value = int.Parse(txtMagv.Text);
            cmd.Parameters.Add("@diemhs1", SqlDbType.Float).Value = float.Parse(txtDiemhs1.Text);
            cmd.Parameters.Add("@diemhs2", SqlDbType.Float).Value = float.Parse(txtDiemhs2.Text);
            cmd.Parameters.Add("@diemthihk", SqlDbType.Float).Value = float.Parse(txtDiemhk.Text);
            //cmd.Parameters.Add("@diemtk", SqlDbType.Float).Value = float.Parse(txtDiemtk.Text);
            cmd.ExecuteNonQuery();
            Form1_Load(sender, e);
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            SqlConnection con = getconnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("Pro_Xoa_DiemHS", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@mahs", SqlDbType.Int).Value = int.Parse(txtMahs.Text);
            cmd.ExecuteNonQuery();
            Form1_Load(sender, e);
        }
        int dong;
        private void dgDiem_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dong = e.RowIndex;
                txtMahs.Text = dgDiem.Rows[dong].Cells[0].Value.ToString();
                txtMamh.Text = dgDiem.Rows[dong].Cells[1].Value.ToString();
                txtMagv.Text = dgDiem.Rows[dong].Cells[2].Value.ToString();
                txtDiemhs1.Text = dgDiem.Rows[dong].Cells[3].Value.ToString();
                txtDiemhs2.Text = dgDiem.Rows[dong].Cells[4].Value.ToString();
                txtDiemhk.Text = dgDiem.Rows[dong].Cells[5].Value.ToString();
                txtDiemtk.Text = dgDiem.Rows[dong].Cells[6].Value.ToString();
            }
            catch(Exception)
            {}
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            FormHeThong ht = new FormHeThong();
            ht.Show();
        }

        private void butTimkiem_Click(object sender, EventArgs e)
        {
            grThongTin1.Enabled = false;
            grThongTin2.Enabled = false;
            grTimKiem.Enabled = true;
        }

        private void rdMaHocSinh_CheckedChanged(object sender, EventArgs e)
        {
            txtTimMahs.Enabled = true;
            txtTimMamh.Enabled = false;
            txtTimMagv.Enabled = false;
        }

        private void rdMaMonHoc_CheckedChanged(object sender, EventArgs e)
        {
            txtTimMahs.Enabled = false;
            txtTimMamh.Enabled = true;
            txtTimMagv.Enabled = false;
        }

        private void rdMaGiaoVien_CheckedChanged(object sender, EventArgs e)
        {
            txtTimMahs.Enabled = false;
            txtTimMamh.Enabled = false;
            txtTimMagv.Enabled = true;
        }

        private void txtTimMahs_TextChanged(object sender, EventArgs e)
        {
            if (txtMahs.Text.Length == 0)
            {
                Form1_Load(sender, e);
            }
            else
                dgDiem.DataSource = ac.GetTable("select mahs,mamh,magv,diemhs1,diemhs2,diemthihk,sum((diemhs1+diemhs2*2+diemthihk*3)/6) as diemtk from tbldiem where mahs='"+txtMahs.Text+"' group by mahs,mamh,magv,diemhs1,diemhs2,diemthihk");
        }
    }
}
